package com.chujiekeji.test.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chujiekeji.test.BaseApplication;
import com.chujiekeji.test.R;
import com.chujiekeji.test.activity.PubActivity;
import com.chujiekeji.test.activity.TaskDetailActivity;
import com.chujiekeji.test.adapter.BaseRecyclerAdapter;
import com.chujiekeji.test.adapter.HomeListAdapter;
import com.chujiekeji.test.api.ChujieApi;
import com.chujiekeji.test.base.BaseConstant;
import com.chujiekeji.test.base.BaseFragment;
import com.chujiekeji.test.base.BaseImageLoader;
import com.chujiekeji.test.base.UBLImageLoader;
import com.chujiekeji.test.bean.ArticleBean;
import com.chujiekeji.test.bean.HomeBean;
import com.chujiekeji.test.bean.PageBean;
import com.chujiekeji.test.bean.ResultBean;
import com.chujiekeji.test.bean.Task;
import com.chujiekeji.test.pictureselector.SimpleActivity;
import com.chujiekeji.test.ui.EmptyLayout;
import com.chujiekeji.test.util.AppOperator;
import com.chujiekeji.test.util.JsonUtil;
import com.chujiekeji.test.util.TLog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sunfusheng.marqueeview.MarqueeView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cz.msebera.android.httpclient.Header;
import io.github.xudaojie.qrcodelib.CaptureActivity;

/**
 * @author MapleStory
 * @ qq 1002285057
 * @ project https://gitee.com/MapStory/Shopwt-Android
 */

public class HomeFragment extends BaseFragment {
    protected TextHttpResponseHandler mHandler;

    @BindView(R.id.mainSwipeRefreshLayout)
    SwipeRefreshLayout mainSwipeRefreshLayout;
    @BindView(R.id.mainScrollView)
    NestedScrollView mainScrollView;
    @BindView(R.id.searchEditText)
    RelativeLayout searchEditText;
    @BindView(R.id.head_rr)
    RelativeLayout messageImageView;
    @BindView(R.id.head_ll)
    LinearLayout scanImageView;

    @BindView(R.id.mainBanner)
    Banner mainBanner;
    @BindView(R.id.navigationLinearLayout)
    LinearLayoutCompat navigationLinearLayout;
    @BindView(R.id.oneLinearLayout)
    LinearLayoutCompat oneLinearLayout;
    @BindView(R.id.oneImageView)
    AppCompatImageView oneImageView;
    @BindView(R.id.oneTextView)
    AppCompatTextView oneTextView;
    @BindView(R.id.twoLinearLayout)
    LinearLayoutCompat twoLinearLayout;
    @BindView(R.id.twoImageView)
    AppCompatImageView twoImageView;
    @BindView(R.id.twoTextView)
    AppCompatTextView twoTextView;
    @BindView(R.id.thrLinearLayout)
    LinearLayoutCompat thrLinearLayout;
    @BindView(R.id.thrImageView)
    AppCompatImageView thrImageView;
    @BindView(R.id.thrTextView)
    AppCompatTextView thrTextView;
    @BindView(R.id.fouLinearLayout)
    LinearLayoutCompat fouLinearLayout;
    @BindView(R.id.fouImageView)
    AppCompatImageView fouImageView;
    @BindView(R.id.fouTextView)
    AppCompatTextView fouTextView;
    @BindView(R.id.fivLinearLayout)
    LinearLayoutCompat fivLinearLayout;
    @BindView(R.id.fivImageView)
    AppCompatImageView fivImageView;
    @BindView(R.id.fivTextView)
    AppCompatTextView fivTextView;
    @BindView(R.id.sixLinearLayout)
    LinearLayoutCompat sixLinearLayout;
    @BindView(R.id.sixImageView)
    AppCompatImageView sixImageView;
    @BindView(R.id.sixTextView)
    AppCompatTextView sixTextView;
    @BindView(R.id.sevLinearLayout)
    LinearLayoutCompat sevLinearLayout;
    @BindView(R.id.sevImageView)
    AppCompatImageView sevImageView;
    @BindView(R.id.sevTextView)
    AppCompatTextView sevTextView;
    @BindView(R.id.eigLinearLayout)
    LinearLayoutCompat eigLinearLayout;
    @BindView(R.id.eigImageView)
    AppCompatImageView eigImageView;
    @BindView(R.id.eigTextView)
    AppCompatTextView eigTextView;
    @BindView(R.id.nigLinearLayout)
    LinearLayoutCompat nigLinearLayout;
    @BindView(R.id.nigImageView)
    AppCompatImageView nigImageView;
    @BindView(R.id.nigTextView)
    AppCompatTextView nigTextView;
    @BindView(R.id.tenLinearLayout)
    LinearLayoutCompat tenLinearLayout;
    @BindView(R.id.tenImageView)
    AppCompatImageView tenImageView;
    @BindView(R.id.tenTextView)
    AppCompatTextView tenTextView;
    @BindView(R.id.noticeMarqueeView)
    MarqueeView noticeMarqueeView;
    @BindView(R.id.noticeTextView)
    AppCompatTextView noticeTextView;
    @BindView(R.id.mainRecyclerView)
    RecyclerView mainRecyclerView;

    private HomeListAdapter mainAdapter;
    private List<Task> mainArrayList;
    List<ArticleBean> articleArrayList;

    @BindView(R.id.head_search_rr)
    View head_search_rr;
    private int mDistanceY;
    int nextPage=1;
    int requestCount=20;
    boolean loadAll = false;
    @Override
    public void initData() {

        articleArrayList = new ArrayList<>();
        mainBanner.setImageLoader(new UBLImageLoader());
        mainBanner.setDelayTime(BaseConstant.TIME_DELAY);
        mainBanner.setIndicatorGravity(BannerConfig.CENTER);
        mainBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);

        mainArrayList = new ArrayList<>();
        mHandler = new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                log("HttpResponseHandler:onStart");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                log("HttpResponseHandler:onFailure responseString:" + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                log("HttpResponseHandler:onSuccess responseString:" + responseString);
                try {
                    ResultBean<PageBean<Task>> resultBean = AppOperator.createGson().fromJson(responseString, new TypeToken<ResultBean<PageBean<Task>>>() {
                    }.getType());
                    if (resultBean != null && resultBean.isSuccess() && resultBean.getResult().getItems() != null) {
                        nextPage = resultBean.getResult().getNextPage();
                        mainAdapter.AddFooterItem(resultBean.getResult().getItems());
                        if (resultBean.getResult().getItems().size()<20){
                            mainAdapter.changeMoreStatus(HomeListAdapter.NO_LOAD_MORE);
                            loadAll = true;
                            return;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    onFailure(statusCode, headers, responseString, e);
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                log("HttpResponseHandler:onFinish");
            }

            @Override
            public void onCancel() {
                super.onCancel();
            }
        };

        mainAdapter = new HomeListAdapter(getActivity(), mainArrayList);
        mainAdapter.setOnItemClickListener(new HomeListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, String msg) {
                Task task = mainArrayList.get(position);
                Intent intent = new Intent(getActivity(), TaskDetailActivity.class);
                intent.putExtra("taskId",task.getId());
                intent.putExtra("nickName",task.getNickName());
                intent.putExtra("content",task.getContent());
                intent.putExtra("moneyReward",task.getMoneyReward());
                intent.putExtra("helpNum",task.getHelpNum());
                intent.putExtra("time",task.getTime());
                startActivity(intent);
            }
        });
        BaseApplication.get().setRecyclerView(getActivity(), mainRecyclerView, mainAdapter,false);
        BaseApplication.get().setSwipeRefreshLayout(mainSwipeRefreshLayout);

        handlerAdvList(null);
        handlerHomeNavigation(null);
        handlerGG(null);
        handlerTask();
    }

    private void handlerTask() {
        ChujieApi.fetchTask(nextPage,requestCount,mHandler);
    }

    @Override
    public void initEven() {
        head_search_rr.setBackgroundColor(Color.argb(0, 255, 75, 0));

        scanImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PubActivity.show(getContext());
            }
        });
        messageImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().start(getActivity(), SimpleActivity.class);
            }
        });
        mainScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView nestedScrollView, int dx, int dy, int i2, int i3) {
                mDistanceY = dy;
                //toolbar的高度
                int toolbarHeight = head_search_rr.getBottom()+30;
                //当滑动的距离 <= toolbar高度的时候，改变Toolbar背景色的透明度，达到渐变的效果
                if (mDistanceY <= toolbarHeight) {
                    float scale = (float) mDistanceY / toolbarHeight;
                    float alpha = scale * 255;
                    head_search_rr.setBackgroundColor(Color.argb((int) alpha, 255, 75, 0));
                } else {
                    //将标题栏的颜色设置为完全不透明状态
                    head_search_rr.setBackgroundResource(R.color.primary);
                }
                if (dy == (nestedScrollView.getChildAt(0).getMeasuredHeight() - nestedScrollView.getMeasuredHeight())) {
                    initLoadMoreListener();//调用刷新控件对应的加载更多方法
                }
            }

        });
        initPullRefresh();

    }
    private void handlerGG(String gg){
        gg="{" +
                "\"code\": 200," +
                "\"datas\": {" +
                "\"article_list\": [{" +
                "\"article_id\": \"35\"," +
                "\"ac_id\": \"1\"," +
                "\"article_url\": \"http://www.shopwt.com/\"," +
                "\"article_show\": \"1\"," +
                "\"article_position\": \"1\"," +
                "\"article_sort\": \"1\"," +
                "\"article_title\": \"ShopWT\\u70ed\\u9500\\u4e2d\"," +
                "\"article_content\": \"ShopWT\\u70ed\\u9500\\u4e2d<br />\"," +
                "\"article_time\": \"1534238696\"" +
                "}, {" +
                "\"article_id\": \"41\"," +
                "\"ac_id\": \"1\"," +
                "\"article_url\": \"\"," +
                "\"article_show\": \"1\"," +
                "\"article_position\": \"1\"," +
                "\"article_sort\": \"255\"," +
                "\"article_title\": \"\\u529f\\u80fd\\u4f7f\\u7528\\u8bf4\\u660e\"," +
                "\"article_content\": \"\\u529f\\u80fd\\u4f7f\\u7528\\u8bf4\\u660e\"," +
                "\"article_time\": \"1512980335\"" +
                "}, {" +
                "\"article_id\": \"38\"," +
                "\"ac_id\": \"1\"," +
                "\"article_url\": \"\"," +
                "\"article_show\": \"1\"," +
                "\"article_position\": \"1\"," +
                "\"article_sort\": \"255\"," +
                "\"article_title\": \"\\u63d0\\u793a\\u4fe1\\u606f\"," +
                "\"article_content\": \"\\u63d0\\u793a\\u4fe1\\u606f\"," +
                "\"article_time\": \"1512980335\"" +
                "}, {" +
                "\"article_id\": \"37\"," +
                "\"ac_id\": \"1\"," +
                "\"article_url\": \"\"," +
                "\"article_show\": \"1\"," +
                "\"article_position\": \"1\"," +
                "\"article_sort\": \"255\"," +
                "\"article_title\": \"\\u5982\\u4f55\\u6269\\u5145\\u6c34\\u5370\\u5b57\\u4f53\\u5e93\"," +
                "\"article_content\": \"\\u5982\\u4f55\\u6269\\u5145\\u6c34\\u5370\\u5b57\\u4f53\\u5e93\"," +
                "\"article_time\": \"1512980335\"" +
                "}, {" +
                "\"article_id\": \"36\"," +
                "\"ac_id\": \"1\"," +
                "\"article_url\": \"\"," +
                "\"article_show\": \"1\"," +
                "\"article_position\": \"1\"," +
                "\"article_sort\": \"255\"," +
                "\"article_title\": \"\\u7ba1\\u7406\\u529f\\u80fd\\u8bf4\\u660e\"," +
                "\"article_content\": \"\\u7ba1\\u7406\\u529f\\u80fd\\u8bf4\\u660e\"," +
                "\"article_time\": \"1512980335\"" +
                "}]" +
                "}" +
                "}";
        articleArrayList.clear();
        List<String> list = new ArrayList<>();
        JSONObject ggJson = null;
        JSONObject dataJson = null;
        JSONArray itemsJson = null;
        Gson gson = new Gson();
        try {
            ggJson = new JSONObject(gg);
            dataJson = ggJson.getJSONObject("datas");
            itemsJson = dataJson.getJSONArray("article_list");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        articleArrayList.addAll((List<ArticleBean>)gson.fromJson(itemsJson.toString(), new TypeToken<List<ArticleBean>>(){}.getType()));
        for (int i = 0; i < articleArrayList.size(); i++) {
            list.add(articleArrayList.get(i).getArticleTitle());
        }
        noticeMarqueeView.startWithList(list);
    }

    private void handlerHomeNavigation(String navigation) {
        navigation="{" +
                "\"title\": \"\"," +
                "\"square_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805206137207296.png\"," +
                "\"square_type\": \"url\"," +
                "\"square_data\": \"html/member/signin.html\"," +
                "\"square_ico_name\": \"身边任务\"," +
                "\"square_ico_color\": \"#AC92ED\"," +
                "\"rectangle1_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805207402490296.png\"," +
                "\"rectangle1_type\": \"url\"," +
                "\"rectangle1_data\": \"html/product_robbuy.html\"," +
                "\"rectangle1_ico_name\": \"学习培训\"," +
                "\"rectangle1_ico_color\": \"#FB6E52\"," +
                "\"rectangle2_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805207885094000.png\"," +
                "\"rectangle2_type\": \"url\"," +
                "\"rectangle2_data\": \"html/product_dazhe.html\"," +
                "\"rectangle2_ico_name\": \"充值取现\"," +
                "\"rectangle2_ico_color\": \"#FD6B9D\"," +
                "\"rectangle3_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805208761194713.png\"," +
                "\"rectangle3_type\": \"url\"," +
                "\"rectangle3_data\": \"html/coupon_list.html\"," +
                "\"rectangle3_ico_name\": \"签到\"," +
                "\"rectangle3_ico_color\": \"#FF9300\"," +
                "\"rectangle4_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05858335170370872.png\"," +
                "\"rectangle4_type\": \"url\"," +
                "\"rectangle4_data\": \"html/pingou_list.html\"," +
                "\"rectangle4_ico_name\": \"爱心筹\"," +
                "\"rectangle4_ico_color\": \"#62BA1E\"," +
                "\"rectangle5_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805218611434845.png\"," +
                "\"rectangle5_type\": \"url\"," +
                "\"rectangle5_data\": \"html/pointspro_list.html\"," +
                "\"rectangle5_ico_name\": \"寻找同窗\"," +
                "\"rectangle5_ico_color\": \"#48CFAE\"," +
                "\"rectangle6_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805219028261820.png\"," +
                "\"rectangle6_type\": \"url\"," +
                "\"rectangle6_data\": \"shop.html\"," +
                "\"rectangle6_ico_name\": \"排行榜单\"," +
                "\"rectangle6_ico_color\": \"#FB7B78\"," +
                "\"rectangle7_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805221986936315.png\"," +
                "\"rectangle7_type\": \"url\"," +
                "\"rectangle7_data\": \"html/red_packet.html?id=1\"," +
                "\"rectangle7_ico_name\": \"旅游攻略\"," +
                "\"rectangle7_ico_color\": \"#DF3F4A\"," +
                "\"rectangle8_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805226514353205.png\"," +
                "\"rectangle8_type\": \"special\"," +
                "\"rectangle8_data\": \"1\"," +
                "\"rectangle8_ico_name\": \"直播\"," +
                "\"rectangle8_ico_color\": \"#37A6FB\"," +
                "\"rectangle9_image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05805230596262289.png\"," +
                "\"rectangle9_type\": \"url\"," +
                "\"rectangle9_data\": \"html/product_list.html\"," +
                "\"rectangle9_ico_name\": \"积分商城\"," +
                "\"rectangle9_ico_color\": \"#FB6E52\"" +
                "}";
        try {
            final JSONObject jsonObject = new JSONObject(navigation);
            //第一个
            final String squareType = jsonObject.getString("square_type");
            final String squareData = jsonObject.getString("square_data");
            oneTextView.setText(jsonObject.getString("square_ico_name"));
            oneImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("square_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("square_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), oneImageView);
            oneLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), squareType, squareData);
                }
            });
            //第二个
            final String rectangle1Type = jsonObject.getString("rectangle1_type");
            final String rectangle1Data = jsonObject.getString("rectangle1_data");
            twoTextView.setText(jsonObject.getString("rectangle1_ico_name"));
            twoImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle1_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle1_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), twoImageView);
            twoLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle1Type, rectangle1Data);
                }
            });
            //第三个
            final String rectangle2Type = jsonObject.getString("rectangle2_type");
            final String rectangle2Data = jsonObject.getString("rectangle2_data");
            thrTextView.setText(jsonObject.getString("rectangle2_ico_name"));
            thrImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle2_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle2_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), thrImageView);
            thrLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle2Type, rectangle2Data);
                }
            });
            //第四个
            final String rectangle3Type = jsonObject.getString("rectangle3_type");
            final String rectangle3Data = jsonObject.getString("rectangle3_data");
            fouTextView.setText(jsonObject.getString("rectangle3_ico_name"));
            fouImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle3_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle3_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), fouImageView);
            fouLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle3Type, rectangle3Data);
                }
            });
            //第五个
            final String rectangle4Type = jsonObject.getString("rectangle4_type");
            final String rectangle4Data = jsonObject.getString("rectangle4_data");
            fivTextView.setText(jsonObject.getString("rectangle4_ico_name"));
            fivImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle4_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle4_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), fivImageView);
            fivLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle4Type, rectangle4Data);
                }
            });
            //第六个
            final String rectangle5Type = jsonObject.getString("rectangle5_type");
            final String rectangle5Data = jsonObject.getString("rectangle5_data");
            sixTextView.setText(jsonObject.getString("rectangle5_ico_name"));
            sixImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle5_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle5_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), sixImageView);
            sixLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle5Type, rectangle5Data);
                }
            });
            //第七个
            final String rectangle6Type = jsonObject.getString("rectangle6_type");
            final String rectangle6Data = jsonObject.getString("rectangle6_data");
            sevTextView.setText(jsonObject.getString("rectangle6_ico_name"));
            sevImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle6_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle6_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), sevImageView);
            sevLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle6Type, rectangle6Data);
                }
            });
            //第八个
            final String rectangle7Type = jsonObject.getString("rectangle7_type");
            final String rectangle7Data = jsonObject.getString("rectangle7_data");
            eigTextView.setText(jsonObject.getString("rectangle7_ico_name"));
            eigImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle7_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle7_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), eigImageView);
            eigLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle7Type, rectangle7Data);
                }
            });
            //第九个
            final String rectangle8Type = jsonObject.getString("rectangle8_type");
            final String rectangle8Data = jsonObject.getString("rectangle8_data");
            nigTextView.setText(jsonObject.getString("rectangle8_ico_name"));
            nigImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle8_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle8_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), nigImageView);
            nigLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle8Type, rectangle8Data);
                }
            });
            //第十个
            final String rectangle9Type = jsonObject.getString("rectangle9_type");
            final String rectangle9Data = jsonObject.getString("rectangle9_data");
            tenTextView.setText(jsonObject.getString("rectangle9_ico_name"));
            tenImageView.setBackgroundDrawable(BaseApplication.get().getGradientDrawable(BaseApplication.get().dipToPx(28), Color.parseColor(jsonObject.getString("rectangle9_ico_color"))));
            BaseImageLoader.get().display(jsonObject.getString("rectangle9_image"), BaseApplication.get().dipToPx(28), BaseApplication.get().dipToPx(28), tenImageView);
            tenLinearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.get().startTypeValue(getActivity(), rectangle9Type, rectangle9Data);
                }
            });
            navigationLinearLayout.setVisibility(View.VISIBLE);
        } catch (JSONException e) {
            navigationLinearLayout.setVisibility(View.GONE);
            e.printStackTrace();
        }

    }

    private void handlerAdvList(String banner) {
        banner="{" +
                "\"item\": [{" +
                "\"image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05665250685953839.png\"," +
                "\"type\": \"keyword\"," +
                "\"data\": \"\\u978b\\u5b50\"" +
                "}, {" +
                "\"image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05665253229871977.png\"," +
                "\"type\": \"keyword\"," +
                "\"data\": \"\\u5973\\u978b\"" +
                "}, {" +
                "\"image\": \"http://demo.shopwt.com/system/upfiles/mobile/special/s0/s0_05665253703663141.jpg\"," +
                "\"type\": \"keyword\"," +
                "\"data\": \"\\u5973\\u978b\"" +
                "}]}";
        try {
            JSONObject jsonObject = new JSONObject(banner);
            ArrayList<HomeBean.AdvListBean> arrayList = new ArrayList<>();
            JSONArray jsonArray = new JSONArray(jsonObject.getString("item"));
            for (int i = 0; i < jsonArray.length(); i++) {
                arrayList.add(JsonUtil.string2Obj(jsonArray.getString(i), HomeBean.AdvListBean.class));
            }
            if (arrayList.size() == 0) {
                mainBanner.setVisibility(View.GONE);
            } else {
                mainBanner.setVisibility(View.VISIBLE);
                List<String> image = new ArrayList<>();
                final List<String> type = new ArrayList<>();
                final List<String> data = new ArrayList<>();
                for (int i = 0; i < arrayList.size(); i++) {
                    image.add(arrayList.get(i).getImage());
                    type.add(arrayList.get(i).getType());
                    data.add(arrayList.get(i).getData());
                }
                mainBanner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        BaseApplication.get().startTypeValue(getActivity(), type.get(position), data.get(position));
                    }
                });
                mainBanner.update(image);
                mainBanner.start();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_home;
    }

    @Override
    public void onStart() {
        super.onStart();
        mainBanner.startAutoPlay();
        noticeMarqueeView.startFlipping();
    }

    @Override
    public void onStop() {
        super.onStop();
        mainBanner.stopAutoPlay();
        noticeMarqueeView.stopFlipping();
    }

    private void initLoadMoreListener() {
        if (loadAll){
            return;
        }
        mainAdapter.changeMoreStatus(HomeListAdapter.LOADING_MORE);
        handlerTask();
    }



    private void initPullRefresh() {
        mainSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        handlerAdvList(null);
                        handlerHomeNavigation(null);
                        handlerGG(null);
                        //刷新完成
                        mainArrayList.clear();
                        nextPage=1;
                        requestCount=20;
                        handlerTask();
                        loadAll=false;
                        mainSwipeRefreshLayout.setRefreshing(false);
                    }
                }, BaseConstant.TIME_REFRESH);
            }

        });
    }

    private void log(String msg) {
        if (true)
            TLog.i("HOMEFragment", msg);
    }

}
